package controller.expertInfo.expertEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.CertificateTypeServices;
import services.expertInfo.ExpertCertificateServices;
import util.JsonUtil;
import validator.EndTimeFluentValidator;
import validator.NotBlankFluentValidator;
import wt.entity.dictionary.CertificateType;
import wt.entity.expertInfo.ExpertCertificate;
import wt.entity.expertInfo.ExpertCertificateExample;

import java.util.List;
import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: expert
 */
@Controller


@RequestMapping("expertInfo/expertEdit/expertCertificate")
public class ExpertSelfCertificateController extends BaseController {

    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;

    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {

        List<ExpertCertificate> expertCertificateList = expertCertificateServices.expertSelectByExpertId(getLoginAccount().getExpertBase().getId());
        expertCertificateServices.lazyLoadCertificateType(expertCertificateList);
        map.put("data", JsonUtil.obj2Json(expertCertificateList));
        return "expertInfo/expertEdit/expertCertificate/index";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("editDlg")
    public String editDlg(String id, Map map) throws Exception {
        if (StringUtils.isNoneBlank(id)) {
            ExpertCertificate expertCertificate = expertCertificateServices.selectByPrimaryKey(id);
            if (expertCertificate == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, expertCertificate);
            map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertCertificate/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertCertificate/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "expertInfo/expertEdit/expertCertificate/editDlg";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ExpertCertificate expertCertificate) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertCertificate.getCertificateTypeId(), new NotBlankFluentValidator("证书类型"))
                .on(expertCertificate.getName(), new NotBlankFluentValidator("证书名称"))
                .on(expertCertificate.getNum(), new NotBlankFluentValidator("证书编号"))
                .on(expertCertificate.getGrade(),new NotBlankFluentValidator("专家类型"))
                .on(expertCertificate.getEndTime(), new EndTimeFluentValidator(expertCertificate.getStartTime(), "发证日期", "有效期至"))
                .doValidate()
                .result(ResultCollectors.toComplex());

        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }

        expertCertificate.setExpertId(getLoginAccount().getExpertBase().getId());
        CertificateType certificateType = certificateTypeServices.selectByPrimaryKey(expertCertificate.getCertificateTypeId());
        expertCertificate.setItemId(certificateType.getItemId());

        ExpertCertificateExample expertCertificateExample = new ExpertCertificateExample();
        expertCertificateExample.createCriteria().andItemIdEqualTo(expertCertificate.getItemId()).andExpertIdEqualTo(expertCertificate.getExpertId());

        List<ExpertCertificate> certificateList = expertCertificateServices.selectByExample(expertCertificateExample);
        if (!certificateList.isEmpty()){
            commonResult.setMessage("该证书类型与已有证书类型互斥！");
            return commonResult.setError();
        }else{
            expertCertificateServices.expertInsertSelective(expertCertificate);
            return commonResult.setSuccess();
        }



    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertCertificate expertCertificate) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertCertificate.getCertificateTypeId(), new NotBlankFluentValidator("证书类型"))
                .on(expertCertificate.getName(), new NotBlankFluentValidator("证书名称"))
                .on(expertCertificate.getNum(), new NotBlankFluentValidator("证书编号"))
                .on(expertCertificate.getGrade(),new NotBlankFluentValidator("专家类型"))
                .on(expertCertificate.getEndTime(), new EndTimeFluentValidator(expertCertificate.getStartTime(), "发证日期", "有效期至"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        expertCertificate.setExpertId(getLoginAccount().getExpertBase().getId());
        CertificateType certificateType = certificateTypeServices.selectByPrimaryKey(expertCertificate.getCertificateTypeId());
        expertCertificate.setItemId(certificateType.getItemId());
        expertCertificateServices.expertUpdateByPrimaryKeySelective(expertCertificate);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        expertCertificateServices.expertDeleteByPrimaryKey(id);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("cancelApply")
    public CommonResult cancelApply(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        int result = expertCertificateServices.expertCancelApplyByPrimaryKey(id);
        commonResult.setStatus(result);
        return commonResult;
    }

}
